using FileServer.Abstractions.Models.Options;
using FileServer.Extension.Providers;
using System.Text.RegularExpressions;

namespace FileServer.Extension;

public class FileUploadProvider
{
    private static List<FileProviderOptions> _uploadFileProviderOptions = new List<FileProviderOptions>();
    public static void SetUploadFileProviderOptions(List<FileProviderOptions> uploadFileProviderOptions)
    {
        _uploadFileProviderOptions = uploadFileProviderOptions;
    }


    /// <summary>
    /// 获取 file provider option 
    /// </summary>
    /// <param name="options"></param>
    /// <param name="storageKey"></param>
    /// <returns></returns>
    public static FileProviderOptions GetFileProviderOptions(string storageKey)
    {
        return _uploadFileProviderOptions.FirstOrDefault(x => storageKey.Equals(x.StorageKey));
    }


    /// <summary>
    /// 获取 file provider option 
    /// </summary>
    /// <param name="options"></param>
    /// <param name="storageKey"></param>
    /// <returns></returns>
    public static FileProviderOptions GetDefaultFileProviderOptions()
    {
        return _uploadFileProviderOptions.FirstOrDefault(x => x.IsDefault);
    }

    private static readonly Dictionary<string, Type> _uploadProviders = [];

    /// <summary>
    /// 查找类型
    /// </summary>
    /// <param name="providerKey"></param>
    /// <returns></returns>
    public static Type Get(string providerKey)
    {
        if (_uploadProviders.TryGetValue(providerKey, out var provider))
        {
            return provider;
        }

        throw new ArgumentException(providerKey + " not found in providers");
    }

    /// <summary>
    /// 添加类
    /// </summary>
    /// <param name="providerKey"></param>
    /// <param name="provider"></param>
    public static void Add(string providerKey, Type provider)
    {
        _uploadProviders.TryAdd(providerKey, provider);
    }


    /// <summary>
    /// 构建默认的服务提供者
    /// </summary>
    public static void BuildDefault()
    {
        Add("local", typeof(LocalFileUploadProvider));
        Add("oss", typeof(OssFileUploadProvider));
    }


    /// <summary>
    /// 本地文件的 文件夹 前缀
    /// </summary>
    public static string LocalFilePerfix = "Private";

    private static Regex _localFileRegex = new Regex(@"\b" + LocalFilePerfix + @"\b");

    public static string GetDomainPath(string path)
    {
        var key = GetLocalFileStorageKey(path);
        var option = GetFileProviderOptions(key);
        return option.Domain + (path.StartsWith("/") ? path : ("/" + path));
    }
    /// <summary>
    /// 判断是否为本地存储
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    public static string GetLocalFileStorageKey(string filePath)
    {
        Match v = _localFileRegex.Match(filePath);
        if (v.Success)
        {
            return "local";
        }
        return "";
    }


    /// <summary>
    /// 获取完整路径
    /// </summary>
    /// <param name="filePath"></param>
    /// <returns></returns>
    public static string GetLocalFullPath(string filePath, FileServerOptions options)
    {
        Match v = _localFileRegex.Match(filePath);
        // 本地
        if (v.Success)
        {
            return options.FilePath + @"/" + filePath;
        }
        // oss
        return "";
    }
}